package com.ruoyi.result.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.result.mapper.ResultPatentMapper;
import com.ruoyi.result.domain.ResultPatent;
import com.ruoyi.result.domain.ResultThesis;
import com.ruoyi.result.service.IResultPatentService;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.util.CollectUtil;

/**
 * 专利Service业务层处理
 * 
 * @author yepanpan
 * @date 2021-03-18
 */
@Service
public class ResultPatentServiceImpl implements IResultPatentService 
{
    @Autowired
    private ResultPatentMapper resultPatentMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询专利
     * 
     * @param id 专利ID
     * @return 专利
     */
    @Override
    public ResultPatent selectResultPatentById(Long id)
    {
    	ResultPatent resultPatent = resultPatentMapper.selectResultPatentById(id);
    	String userIds = resultPatent.getUserIds();
    	userIds = userIds.substring(1,userIds.length()-1);
    	String[] usersId = userIds.split(",");
    	Long userId[] = new Long[usersId.length];
    	String userNames = "";
    	for(int i=0;i<usersId.length;i++) {
    		userId[i] = Long.parseLong(usersId[i]);
    		SysUser user = sysUserMapper.selectUserById(userId[i]);
    		if(user != null) {
    			userNames += ","+user.getNickName();
    		}
    	}
    	resultPatent.setUserNames(userNames.substring(1));
    	resultPatent.setUserId(userId);
    	return resultPatent;
    }

    /**
     * 查询专利列表
     * 
     * @param resultPatent 专利
     * @return 专利
     */
    @Override
    public List<ResultPatent> selectResultPatentList(ResultPatent resultPatent)
    {
    	List<ResultPatent> list = resultPatentMapper.selectResultPatentList(resultPatent);
    	for(ResultPatent row : list) {
    		String userIds = row.getUserIds();
        	userIds = userIds.substring(1,userIds.length()-1);
        	String[] usersId = userIds.split(",");
        	Long userId[] = new Long[usersId.length];
        	String userNames = "";
        	for(int i=0;i<usersId.length;i++) {
        		userId[i] = Long.parseLong(usersId[i]);
        		SysUser user = sysUserMapper.selectUserById(userId[i]);
        		if(user != null) {
        			userNames += ","+user.getNickName();
        		}
        	}
        	row.setUserNames(userNames.substring(1));
        	row.setUserId(userId);
    	}
    	return list;
    }

    /**
     * 新增专利
     * 
     * @param resultPatent 专利
     * @return 结果
     */
    @Override
    public int insertResultPatent(ResultPatent resultPatent)
    {
    	if(resultPatent.getUserId() != null) {
    		resultPatent.setUserIds(CollectUtil.joins(resultPatent.getUserId(), ","));
		}else {
			throw new CustomException("本校获奖人员不能为空");
		}   
        resultPatent.setCreateTime(DateUtils.getNowDate());
        resultPatent.setCreateBy(SecurityUtils.getUsername());
        return resultPatentMapper.insertResultPatent(resultPatent);
    }

    /**
     * 修改专利
     * 
     * @param resultPatent 专利
     * @return 结果
     */
    @Override
    public int updateResultPatent(ResultPatent resultPatent)
    {
    	if(resultPatent.getUserId() != null) {
    		resultPatent.setUserIds(CollectUtil.joins(resultPatent.getUserId(), ","));
		}else {
			throw new CustomException("本校获奖人员不能为空");
		}   
        resultPatent.setUpdateTime(DateUtils.getNowDate());
        resultPatent.setUpdateBy(SecurityUtils.getUsername());
        return resultPatentMapper.updateResultPatent(resultPatent);
    }

    /**
     * 批量删除专利
     * 
     * @param ids 需要删除的专利ID
     * @return 结果
     */
    @Override
    public int deleteResultPatentByIds(Long[] ids)
    {
        return resultPatentMapper.deleteResultPatentByIds(ids);
    }

    /**
     * 删除专利信息
     * 
     * @param id 专利ID
     * @return 结果
     */
    @Override
    public int deleteResultPatentById(Long id)
    {
        return resultPatentMapper.deleteResultPatentById(id);
    }
}
